*&---------------------------------------------------------------------*
*& Report ZEX_LISTING_616                                              *
*&---------------------------------------------------------------------*
*& Created By: James Wood (james.wood@bowdarkconsulting.com)           *
*& Created On: 12/12/2008                                              *
*& Purpose:    This program demonstrates the use of the ZCL_VECTOR     *
*&             class described in Section 6.3.4.                       *
*&---------------------------------------------------------------------*
REPORT zex_listing_616.

*----------------------------------------------------------------------*
* START-OF-SELECTION Event Module                                      *
*----------------------------------------------------------------------*
START-OF-SELECTION.
  PERFORM test_vector.

*&---------------------------------------------------------------------*
*&      Form  test_vector
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM test_vector.

* Local Data Declarations:
  DATA: lr_customer1 TYPE REF TO zcl_customer,
        lr_customer2 TYPE REF TO zcl_customer,
        lr_customer3 TYPE REF TO zcl_customer,
        lr_vector    TYPE REF TO zcl_vector.

* Create three sample customers:
  CREATE OBJECT lr_customer1
    EXPORTING
      im_name = 'Andrea'.

  CREATE OBJECT lr_customer2
    EXPORTING
      im_name = 'Andersen'.

  CREATE OBJECT lr_customer3
    EXPORTING
      im_name = 'Paige'.

* Add the customers to the vector container in
* random order:
  CREATE OBJECT lr_vector.

  CALL METHOD lr_vector->add
    EXPORTING
      im_object = lr_customer2.

  CALL METHOD lr_vector->add
    EXPORTING
      im_object = lr_customer3.

  CALL METHOD lr_vector->add
    EXPORTING
      im_object = lr_customer1.

* Show the customers before the sort operation:
  PERFORM show_customers USING lr_vector.

* Now, sort the customers:
  CALL METHOD lr_vector->sort( ).
  SKIP.

* Show the customers after the sort operation:
  PERFORM show_customers USING lr_vector.

ENDFORM.                    "test_vector

*&---------------------------------------------------------------------*
*&      Form  show_customers
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->IM_VECTOR  text
*----------------------------------------------------------------------*
FORM show_customers USING im_vector TYPE REF TO zcl_vector.

* Local Data Declarations:
  DATA: lr_iterator TYPE REF TO cl_swf_utl_iterator,
        lv_count TYPE i,
        lr_object TYPE REF TO object,
        lr_customer TYPE REF TO zcl_customer.

  lr_iterator = im_vector->iterator( ).
  lv_count = lr_iterator->get_count( ).

  DO lv_count TIMES.
    lr_object = lr_iterator->get_current( ).
    lr_customer ?= lr_object.
    lr_customer->display( ).
    lr_iterator->get_next( ).
  ENDDO.

ENDFORM.                    "show_customers